package com.zhongshengnetwork.rightbe.my.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.thinkcool.circletextimageview.CircleTextImageView;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.dbservice.ShopCardbService;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.my.model.MeModel;

import java.util.ArrayList;
import java.util.List;

public class MyCenterActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private static final int REQUEST_CODE_LOGOUT = 0x1001;
    private TopBarView topbar;
    private List<MeModel> list=null;
    private ListView listview;
    private Button myButton;
    private ImageView header;
    private TextView nickname;
    private TextView login;
    private ImageView qrcode;
    private ImageView next;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_center);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        initData();
        listview=(ListView) findViewById(R.id.my_listview);
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        header = (ImageView) findViewById(R.id.my_list_myheader);
        nickname = (TextView) findViewById(R.id.my_list_mynickname);
        login = (TextView) findViewById(R.id.my_list_login);
        qrcode = (ImageView) findViewById(R.id.my_list_mycode);
        next = (ImageView) findViewById(R.id.my_list_mynext);
        myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CustomApplication.loginModel.getToken() == null) {//还没登录
                    //System.out.print("还没登录\n");
                    startActivity(new Intent(CustomApplication.mContext, LoginActivity.class));
                }else {//进入个人中心
                    //System.out.print("进入个人中心\n");
                    startActivity(new Intent(CustomApplication.mContext, MyInfoActivity.class));
                }
            }
        });
        initHeaerView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initHeaerView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == REQUEST_CODE_LOGOUT){
                finish();
            }
        }
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    /**
     * 初始化UI
     */
    public void initHeaerView()
    {
        if (login == null){
            return;
        }
        if (CustomApplication.loginModel.getToken() == null){//还没登录
            login.setVisibility(View.VISIBLE);
            header.setVisibility(View.INVISIBLE);
            nickname.setVisibility(View.INVISIBLE);
            qrcode.setVisibility(View.INVISIBLE);
            next.setVisibility(View.INVISIBLE);
        }else {
            login.setVisibility(View.INVISIBLE);
            header.setVisibility(View.VISIBLE);
            Glide.with(this).load(CustomApplication.loginModel.getThumbheader()).transform(new CircleTransform(MyCenterActivity.this)).dontAnimate().into(header);
            nickname.setVisibility(View.VISIBLE);
            nickname.setText(CustomApplication.loginModel.getNickname());
            qrcode.setVisibility(View.INVISIBLE);
            next.setVisibility(View.VISIBLE);
        }
    }

    public void update(boolean isLogin){
        if (list != null){
            MeModel model = list.get(3);
            if (CommonUtils.isLogin() == true){
                long count = ShopCardbService.getShopCarCount(CustomApplication.loginModel.getUid());
                model.setCount((int)count);
            }else {
                model.setCount(0);
            }
            list.set(3,model);
            adapter.notifyDataSetChanged();
        }
    }

    public void initData(){
        list = new ArrayList<MeModel>();
        MeModel model = new MeModel();
        model = new MeModel();
        model.setType(0);
        list.add(model);
        model = new MeModel();
        model.setType(1);
        model.setCount(0);
        model.setTitle("设置");
        list.add(model);

        model = new MeModel();
        model.setType(0);
        list.add(model);
        model = new MeModel();
        model.setType(1);
        model.setCount(0);
        model.setTitle("重要说明");
        list.add(model);
    }

    private class MyAdapter extends BaseAdapter implements View.OnClickListener {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            if(list.get(position).getType() == 0){
                return false;
            }
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            MeModel model = list.get(position);
            View view=convertView;
            if(model.getType() == 0){
                view= LayoutInflater.from(CustomApplication.mContext).inflate(R.layout.my_list_heaer, null);
                TextView text = (TextView) view.findViewById(R.id.my_list_header_text);
                text.setText("");
            }else{
                view=LayoutInflater.from(CustomApplication.mContext).inflate(R.layout.my_list_item, null);
                TextView text=(TextView) view.findViewById(R.id.my_list_item_text);
                text.setText(model.getTitle());
                ImageView icon = (ImageView) view.findViewById(R.id.my_list_item_icon);

                if (position == 1){
                    icon.setImageResource(R.drawable.setting);
                }else if (position == 3){
                    icon.setImageResource(R.drawable.notice);
                }
                //icon.setImageResource(R.drawable.find006);
                ImageView next = (ImageView) view.findViewById(R.id.my_list_item_next);
                next.setImageResource(R.drawable.right);
                CircleTextImageView countText = (CircleTextImageView) view.findViewById(R.id.my_list_item_count);
                if (model.getCount() > 0){
                    countText.setVisibility(View.VISIBLE);
                    countText.setText(model.getCount()+"");
                }else {
                    countText.setVisibility(View.INVISIBLE);
                }
                text.setTag(position);
                text.setOnClickListener(this);
            }
            return view;
        }

        // 监听事件实现
        public void onClick(View v) {
            int destination = (Integer) v.getTag();
            System.out.print("onClick:"+destination+"\n");
            //startActivity(new Intent(CustomApplication.mContext, LoginActivity.class));

            switch (destination) {
                case 0:

                    break;
                case 1:
                    if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
                        startActivity(new Intent(CustomApplication.mContext, LoginActivity.class));
                    }else {
                        startActivityForResult(new Intent(CustomApplication.mContext, SettingsActivity.class),REQUEST_CODE_LOGOUT);
                    }

                    break;

                case 3://
                    if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
                        startActivity(new Intent(CustomApplication.mContext, LoginActivity.class));
                    }else {
                        //startActivity(new Intent(CustomApplication.mContext, DeclareActivity.class));
                        //getActivity().startActivity(new Intent(getContext(), MyShopActivity.class));
                        Intent webIntent = new Intent(MyCenterActivity.this, MyWebActivity.class);
                        webIntent.putExtra(APIKey.urlKey, Constant.HOST+"api/system/note.do");
                        startActivity(webIntent);
                    }
                    break;

                default:

                    break;
            }

        }
    }

}
